﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using Caliburn.Micro;
using MessageLib;
using MessageLib.Frame;
using MessageLib.Frame.InnerFrame;
using VoiceMessager.Models;

namespace VoiceMessager.ViewModels
{
	public sealed class MainViewModel: Screen
	{
		private readonly SimpleContainer _container;
		private readonly IWindowManager _windowManager;
		private readonly CurrentSelection _currentSelection;
		public Udp Udp { get; set; }

	    public BindableCollection<FrameListItemViewModel> FrameList { get; } =
	        new BindableCollection<FrameListItemViewModel>();


        public MainViewModel()
		{
		}

		public MainViewModel(SimpleContainer container, IWindowManager windowManager, CurrentSelection currentSelection)
		{
			_container = container;
			_windowManager = windowManager;
			_currentSelection = currentSelection;
		    try
		    {
		        IpEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.88"), 88);
            }
		    catch (SocketException e)
		    {
		        MessageBox.Show("无网络连接");
		    }
			
			VerifyMethod = VerifyMethod.None;
			DisplayName = @"语音模块-调试工具 V1.0";

		}

		public void ShowConnect() => _windowManager.ShowDialog(_container.GetInstance<ConnectViewModel>());
		public void ShowVerify() => _windowManager.ShowDialog(_container.GetInstance<VerifyViewModel>());
		public void ShowVoice() => _windowManager.ShowDialog(_container.GetInstance<SendFrameViewModel>());

		private IPEndPoint _ipEndPoint;

		public IPEndPoint IpEndPoint
		{
			get => _ipEndPoint;
		    set
			{
				_ipEndPoint = value;
				_currentSelection.IpEndPoint = value;
				ShowConnectContent = $"    对方地址\n{IpEndPoint}";
				Udp = new Udp(_currentSelection.IpEndPoint);
				Udp.OnRecive += _udp_OnRecive;
                Udp.OnSend += Udp_OnSend;
			}
		}

        private void Udp_OnSend(object sender, OnSendEventArgs e)
        {
            FrameList.Add(new FrameListItemViewModel
            {
                Frame = e.Frame,
                SendState = e.SentCount>0?SendState.Sent : SendState.Fail,
                TimeStamp = DateTime.Now
            });
        }

	    private void _udp_OnRecive(object sender, OnReciveEventArgs e)
	    {
	        FrameList.Add(new FrameListItemViewModel
	        {
	            Frame = e.Frame,
                SendState = SendState.Recived,
                TimeStamp = DateTime.Now
	        });
	    }

        private string _showConnectContent;

		public string ShowConnectContent
		{
			get => _showConnectContent;
		    set
			{
				_showConnectContent = value;
				NotifyOfPropertyChange();
			}
		}

		private string _showVerifyContent;

		public string ShowVerifyContent
		{
			get => _showVerifyContent;
		    set
			{
				_showVerifyContent = value;
				NotifyOfPropertyChange();
			}
		}

		private VerifyMethod _verifyMethod;

		public VerifyMethod VerifyMethod
		{
			get => _verifyMethod;
		    set
			{
				_verifyMethod = value;
				_currentSelection.VerifyMethod = value;
				ShowVerifyContent = $"   校验方式\n     {this.VerifyMethod}";
			}
		}

	    public void ButtonKeep()
	    {
	        FrameList.Add(new FrameListItemViewModel
	        {
	            Frame = new ControlCommandFrame()
	        });
	    }

	    public void ShowClear()
	    {
	        this.FrameList.Clear();
	    }

	}
}
